package com.oocl.RTManageServer.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import com.oocl.RTManageServer.manager.ChatServerManager;
import com.oocl.RTManageServer.manager.RTServerManager;
import com.oocl.RTManageServer.manager.impl.ChatServerManagerImpl;
import com.oocl.RTManageServer.manager.impl.RTServerManagerImpl;
import com.oocl.RTManageServer.parser.HttpRequestParser;
import com.oocl.RTManageServer.parser.impl.HttpRequestParserImpl;
import com.oocl.RTManageServer.responser.HttpResponser;
import com.oocl.RTManageServer.responser.impl.HttpResponserImpl;

public class ServerThread extends Thread{
	private Socket socket;
	private BufferedReader reader;
	private OutputStream out;
	private HttpRequestParser parser;
	private ChatServerManager chatServerManager;
	private RTServerManager rtServerManager;
	private HttpResponser responser;
	
	public ServerThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = socket.getOutputStream();
			responser = new HttpResponserImpl(out);
			parser = new HttpRequestParserImpl();
			chatServerManager = new ChatServerManagerImpl(responser);
			rtServerManager = new RTServerManagerImpl(responser);
			
			String line = reader.readLine();
			if(null==line){
				return;
			}
			String request = parser.parse(line.trim());
			System.out.println("re->"+request);
			switch (request) {
			case "/startLogin":
				rtServerManager.processStart();
				break;
			case "/stopLogin":
				rtServerManager.processStop();	
				break;
			case "/startChat":
				chatServerManager.processStart();
				break;
			case "/stopChat":
				chatServerManager.processStop();
				break;
			case "/loadUsers":
				chatServerManager.getUserList();
				break;
			case "/getLoginTime":
				rtServerManager.getServerTime();
				break;
			case "/getChatTime":
				chatServerManager.getServerTime();
				break;
			case "/":
				chatServerManager.defaultView();
				break;
			default:
				
				break;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
